package com.tl.juc.c1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
@Slf4j
public class CallableFuture {
    public static void main(String[] args) {
        // 第一步，创建Callable实现类的实例，实现call方法
//        class Task implements Callable<Integer> {
//
//            @Override
//            public Integer call() throws Exception {
//                return 5;
//            }
//        }
//
//        //第二步，实例Callable
//        Task task = new Task();

        // 第三步，使用FutureTask类包装Callable对象
        // FutureTask<Integer> futureTask = new FutureTask<>(task);
        FutureTask<Integer> futureTask = new FutureTask<>(()->{
            log.info("子线程启动了");
            Thread.sleep(5000);
            return 5;
        });

        // 第四步，线程启动
        new Thread(futureTask).start();
        log.info("已启动");
        try {
            Integer value = futureTask.get();
            log.info("返回值："+value);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
